package com.wyx.springbootspringkafkademo.interceptor;

import org.apache.kafka.clients.producer.Partitioner;
import org.apache.kafka.common.Cluster;
import org.apache.kafka.common.PartitionInfo;

import java.util.List;
import java.util.Map;

/**
 * ClassName MyPartition
 * Description TODO
 *
 * @author qq
 * @date 2025/4/23 18:06
 * @since JDK 1.8
 */
public class MyPartition implements Partitioner {
    @Override
    public int partition(String topic, Object key, byte[] keyByte, Object value, byte[] valueByte, Cluster cluster) {
        List<PartitionInfo> partitionInfos=cluster.availablePartitionsForTopic(topic);
        //返回固定一个partition，保证消费有序性
        return partitionInfos.get(0).partition();
        //轮询返回
//        return key.hashCode()%partitionInfos.size();
    }

    @Override
    public void close() {

    }

    @Override
    public void configure(Map<String, ?> map) {

    }
}
